package com.ds.infrastructure.hyperspace.console.model.request.container;

import com.ds.infrastructure.hyperspace.console.dto.configdata.ConfigDataDTO;
import com.ds.infrastructure.hyperspace.console.dto.configdata.ContainerDataDTO;
import com.ds.infrastructure.hyperspace.console.entity.table.TbContainerConfig;
import com.ds.infrastructure.hyperspace.console.exceptions.HyperspaceException;
import com.ds.infrastructure.hyperspace.console.model.request.containerconfig.configdata.ConfigDataForm;
import com.ds.infrastructure.hyperspace.console.utils.FastJsonUtil;
import com.ds.infrastructure.hyperspace.console.utils.ValidationUtil;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author: yaozhendong
 * @create: 2019-10-16 16:39
 **/
@Data
@NoArgsConstructor
public class SaveContainerForm {
    @NotNull
    private Long id;
    @ApiModelProperty(example = "2018-10-01 12:18:48")
    private Date gmtModified;
    private Integer configVersion;
    List<ConfigDataForm> container = new ArrayList<>();

    public static TbContainerConfig toEntity(SaveContainerForm form) {
        TbContainerConfig tbContainerConfig = new TbContainerConfig();
        tbContainerConfig.setId(form.getId());
        tbContainerConfig.setGmtModified(form.getGmtModified());
        ContainerDataDTO containerDataDTO = SaveContainerForm.toDTO(form);
        tbContainerConfig.setConfigData(FastJsonUtil.serialize(containerDataDTO));
        return tbContainerConfig;
    }

    private static ContainerDataDTO toDTO(SaveContainerForm form) {
        ContainerDataDTO containerDataDTO = new ContainerDataDTO();
        if (form.getContainer() == null || form.getContainer().isEmpty()) {
            return containerDataDTO;
        }
        ValidationUtil.validateEntityList(form.getContainer(), "container");
        List<ConfigDataDTO> configDataDTOList = new ArrayList<>();
        form.getContainer().forEach(item -> configDataDTOList.add(ConfigDataForm.toDTO(item)));
        containerDataDTO.setContainer(configDataDTOList);
        return containerDataDTO;
    }

    public static SaveContainerForm toVO(TbContainerConfig entity) {
        SaveContainerForm saveContainerForm = new SaveContainerForm();
        saveContainerForm.setId(entity.getId());
        saveContainerForm.setGmtModified(entity.getGmtModified());
        saveContainerForm.setConfigVersion(entity.getConfigVersion());
        ContainerDataDTO containerDataDTO = FastJsonUtil.deserialize(entity.getConfigData(), ContainerDataDTO.class);
        if (containerDataDTO != null && !containerDataDTO.getContainer().isEmpty()) {
            List<ConfigDataForm> configDataList = new ArrayList<>();
            containerDataDTO.getContainer().forEach(item -> configDataList.add(ConfigDataForm.toVO(item)));
            saveContainerForm.setContainer(configDataList);
        }
        return saveContainerForm;
    }

    /**
     * 配置数据检查
     *
     * @param entity
     */
    public static void checkConfigData(TbContainerConfig entity) {
        if (entity == null) {
            throw new HyperspaceException("没有找到对应的网关配置数据");
        }
        SaveContainerForm form = toVO(entity);
        if (form.getContainer() == null || form.getContainer().isEmpty()) {
            throw new HyperspaceException("网关配置数据为空");
        }
        form.getContainer().forEach(item -> {
            String proxyName = item.getProxyName();
            if (item.getEntryEndpoints() == null || item.getEntryEndpoints().isEmpty()) {
                throw new HyperspaceException(String.format("Proxy=%s,EntryEndpoints集合数据为空", proxyName));
            }
            if (item.getTargetEndpoints() == null || item.getTargetEndpoints().isEmpty()) {
                throw new HyperspaceException(String.format("Proxy=%s,TargetEndpoints集合数据为空", proxyName));
            }
            if (item.getPipelines() == null || item.getPipelines().isEmpty()) {
                throw new HyperspaceException(String.format("Proxy=%s,Pipeline集合数据为空", proxyName));
            }
        });
    }
}
